<template>
  <div class="page-filter">
    <div class="page-filter-left">
      <a-button type="primary" @click="addFilmMakerClass">添加地区</a-button>
      <a-button @click="multiImport">批量导入</a-button>
      <a-button danger @click="deleteClass">删除</a-button>
    </div>
    <div class="page-filter-right">
      <div class="show">
        <a-input
          :maxlength="19"
          :onkeyup="
            () => {
              if (areaState.area_id) areaState.area_id = areaState.area_id.replace(/\D/g, '')
            }
          "
          :onafterpaste="
            () => {
              if (areaState.area_id) areaState.area_id = areaState.area_id.replace(/\D/g, '')
            }
          "
          v-model:value.trim="areaState.area_id"
          class="input-m-r-16 w-[250px]"
          placeholder="请输入地区ID"
        />
        <a-input
          v-model:value.trim="areaState.keyword"
          class="input-m-r-16 w-[250px]"
          placeholder="请输入地区名称关键词"
        />
        <a-button type="primary" class="m-r-8" @click="handleSearch">搜索</a-button>
        <a-button class="btn-m-r-24" @click="handleReset">重置</a-button>
      </div>
    </div>
  </div>
  <!-- 删除对话框 -->
  <modal-view
    title="提示"
    v-model:visible="deleteVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="deleteConfirm"
    @cancel="deleteCancel"
  >
    <span>确定要删除该分类吗</span>
  </modal-view>
</template>
<script lang="ts" setup>
import { message } from 'ant-design-vue'
import { areaState, resetState, statusState } from '../state'
import ModalView from '@/components/components/modal/ModalView.vue'
import apis from '@/apis'
import { onMounted, ref } from 'vue'
const {
  site: { area_del }
} = apis
const emit = defineEmits(['update-list'])
const deleteVisible = ref(false)
//批量删除分类
const deleteClass = () => {
  if (!statusState.selectedRowKeys.length) return message.warning('请选择至少一个地区进行删除')
  console.log(statusState.selectedRowKeys, '行删除id')
  deleteVisible.value = true
}
//添加分类
const addFilmMakerClass = () => {
  statusState.formVisible = true
  statusState.isAdd = true
}
/**
  删除分类对话框事件
 */
const deleteConfirm = (value: any) => {
  console.log('value: ', value)
  area_del({ area_id: statusState.selectedRowKeys }).then(() => {
    message.success('批量删除成功')
    deleteVisible.value = value
    emit('update-list')
  })
}
const deleteCancel = () => {
  deleteVisible.value = false
  statusState.selectedRowKeys = []
}
const handleSearch = () => {
  console.log(areaState)
  emit('update-list')
}
const handleReset = () => {
  resetState()
  console.log(areaState)
  emit('update-list')
}

const multiImport = () => {
  statusState.batchVisible = true
}
onMounted(() => {
  statusState.selectedRowKeys = []
  resetState()
})
</script>

<style lang="less" scoped>
@common-color: #0083ff; // sea blue

.page-filter {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  &-left {
    display: flex;
    flex-wrap: nowrap;

    margin-top: 10px;

    .ant-btn:not(:last-child) {
      margin-right: 16px;
    }

    .ant-btn:last-child {
      background-color: rgba(254, 76, 77, 0.08);
      margin-right: 16px;
    }
  }

  &-right {
    display: flex;
    align-items: center;

    .select-m-r-24 {
      width: 196px;
      height: 32px;
      margin: 10px 24px 0 0;
    }

    .input-m-r-16 {
      height: 32px;
      margin: 10px 16px 0 0;
    }

    .btn-m-r-24 {
      margin: 10px 24px 0 0;
    }

    .m-r-8 {
      margin: 10px 8px 0 0;
    }
  }
}
</style>
